package top.lyc829.aircontroller.ui.searchVideo;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import top.lyc829.aircontroller.R;
import top.lyc829.aircontroller.logic.dao.Video;
import top.lyc829.aircontroller.ui.videoCache.VideoCacheActivity;
import top.lyc829.aircontroller.ui.vidoeDetial.VideoDetailActivity;
import top.lyc829.aircontroller.ui.BaseActivity;

public class SearchVideoActivity extends BaseActivity {

    private EditText searchText;
    private TextView cacheButton;
    private ListView listView;
    private VideoListAdapter videoVideoListAdapter;
    private List<Video> videoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_video);
        init();
    }

    private void init(){
        videoList = new ArrayList<>();
        searchText = findViewById(R.id.search_text);
        searchText.setOnEditorActionListener(editorActionListener);
        listView = findViewById(R.id.video_list);
        listView.setOnItemClickListener(adapterListener);
        cacheButton = findViewById(R.id.cache_button);
        cacheButton.setOnClickListener(onClickListener);
        videoVideoListAdapter = new VideoListAdapter(this,R.layout.video_list_item,videoList);
        listView.setAdapter(videoVideoListAdapter);
        getVideoList("计组");
    }

    /*
     * 缓存按钮点击事件
     */
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), VideoCacheActivity.class));
        }
    };


    /*
     * 搜索框回车监听
     */
    private TextView.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                //隐藏软键盘
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                String keyword = searchText.getText().toString();
                getVideoList(keyword);
            }
            return false;
        }
    };

    /*
     * 调用后台接口，获取视频列表
     */
    private void getVideoList(String keyword){
        doGet("searchVideo?keyword=" + keyword,Video.class);
    }


    /*
     * 网络请求回调函数，更新界面数据
     */
    @Override
    protected void netCallBack(Object data) {
        List<Video> list = (List<Video>) data;
        videoList.clear();
        videoList.addAll(list);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                videoVideoListAdapter.notifyDataSetChanged();
            }
        });
    }


    /*
     * 列表点击事件，进入视频详细信息界面
     */
    private AdapterView.OnItemClickListener adapterListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Video video = videoList.get(position);
            Intent intent = new Intent(getApplicationContext(), VideoDetailActivity.class);
            intent.putExtra("name",video.getName());
            intent.putExtra("url",video.getLink());
            startActivity(intent);
        }
    };

}